"""
Case Type   : SQL语法
Case Name   : 兼容PG库中查看内置函数quote_literal针对传入的空字符串的返回结果
Create At   : 2023/02/06
Owner       : cr13
Description :
    1.创建兼容PG库
    2.PG库中查看内置函数quote_literal针对传入的空字符串的返回结果
    3.清理环境
Expect      :
    1.成功
    2.返回''
    3.成功
History     :
"""

import os
import unittest
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.commsh = CommonSH('PrimaryDbUser')
        self.database = 'd_ddl_database_0021'

    def test_gs_upgradectl(self):
        text = "----step1:创建兼容PG库 expect:成功----"
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.database};" \
            f"create database {self.database} dbcompatibility = 'PG';"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, result, '执行失败' +
                      text)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, result, '执行失败'
                      + text)

        text = "-----step2:PG库中查看内置函数quote_literal针对传入的空字符串" \
               "的返回结果 expect:返回''-----"
        self.log.info(text)
        sql_cmd = f"select quote_literal('');"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertEqual('', str(result.splitlines()[-2]).strip(), '执行失败' +
                         text)

    def tearDown(self):
        text = '-----step3:清理环境 expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.database};"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, result, '执行失败' +
                      text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
